package com.hxy.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.common.JsonResult;
import com.hxy.pojo.vo.BranchVo;
import com.hxy.service.BranchService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author feiyu
 * 部门表controller
 */
@RestController
@RequestMapping("/branch")
public class BranchController {
    @Autowired
    private BranchService branchService;

    /**
     * 部门表 模糊/全部查询
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/show")
    @ResponseBody
    public JsonResult showBranch(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
                                 @RequestParam(value = "name", required = false, defaultValue = "") String name,
                                 @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
                                 @RequestParam(value = "branch", required = false, defaultValue = "") Integer branch) {
        System.out.println(page+ limit+name+phone+branch);
        page = page == null ? 1 : page;
        PageInfo<BranchVo> BranchPageInfo = branchService.selectAllPagerBranch(page, limit, name, phone, branch);
        return new JsonResult(0, "success", BranchPageInfo);
    }

    /**
     * 删除部门(接受branchid)
     * 1.根据branchid改员工表的Branch为未分配
     * 2.根据branchid改职位表的Branch_Id
     * 3.删除branch的对应部门
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public JsonResult deleteBranch(@RequestParam(value = "id") Integer id) {
        System.out.println("branch/delete:" + id);
        int i = branchService.deleteByPrimaryKey(id);
        if (i > 0) {
            return new JsonResult(1, "删除成功", i);
        } else {
            return new JsonResult(0, "删除失败", i);
        }
    }

    /**
     * 根据id信息回显
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectOneBranchInfo")
    public JsonResult selectOneBranchInfo(@RequestParam(value = "id") Integer id) {
        System.out.println("部门单个信息回显   /branch/selectOneBranchInfo:" + id);
        BranchVo branchVoOne = branchService.selectOneBranchInfo(id);
        if (branchVoOne == null) {
            return new JsonResult(0, "fail", null);
        } else {
            return new JsonResult(0, "success", branchVoOne);
        }
    }

    /**
     * 更新部门信息
     *
     * @param id
     * @param branchname
     * @param bphone
     * @param stuffName
     * @return
     */
    @RequestMapping("/updateOneBranchInfo")
    private JsonResult updateOneBranchInfo(@RequestParam(value = "id") Integer id,
                                           @RequestParam(value = "branchname") String branchname,
                                           @RequestParam(value = "bphone") String bphone,
                                           @RequestParam(value = "stuffName") String stuffName,
                                           @RequestParam(value = "stuffPhone") String stuffPhone
    ) {
        //封装数据
        BranchVo branchVo = new BranchVo();
        branchVo.setId(id);
        branchVo.setBranchname(branchname);
        branchVo.setBphone(bphone);
        branchVo.setStuffName(stuffName);
        branchVo.setStuffPhone(stuffPhone);
        System.out.println("修改的数据: //updateOneBranchInfo" + branchVo);
        int i = branchService.updateOneBranchInfo(branchVo);
        if (i > 0) {
            return new JsonResult(0, "添加成功", i);
        } else {
            return new JsonResult(1, "添加失败(员工不存在)", i);
        }
    }

    /**
     * 添加一条部门信息
     *
     * @param branchVo
     * @return
     */
    @RequestMapping("/addOneBranchInfo")
    public JsonResult addOneBranchInfo(BranchVo branchVo) {

        System.out.println("部门添加数据://addOneBranchInfo:" + branchVo);
        String msg = branchService.addOneBranchInfo(branchVo);
        return new JsonResult(0, msg, "addOneBranchInfo");
    }

    /**
     * 检查表单输入 1
     *
     * @param stuffName
     * @param stuffPhone
     * @return
     */
    @RequestMapping("/ischeckBEid")
    public JsonResult isCheckBEid(@RequestParam("stuffName") String stuffName,
                                  @RequestParam("stuffPhone") String stuffPhone) {
        System.out.println("表单校验stuffName/phone" + stuffName + stuffPhone);
        BranchVo branchVo = new BranchVo();
        branchVo.setStuffName(stuffName);
        branchVo.setStuffPhone(stuffPhone);
        //执行标志码 1 执行ischeckBEid 2执行ischeckBranchNameNum  3执行ischeckBranchPhoneNum
        int Checkflag = 1;
        try {
            JsonResult jsonResult = branchService.checkAddForm(branchVo, Checkflag);
            return jsonResult;
        }
        catch (Exception e){
            return new JsonResult(1,"数据异常",null);
        }
    }
    /**
     * 检查表单输入 2
     *
     * @param branchname
     * @return
     */
    @RequestMapping("/ischeckBranchNameNum")
    public JsonResult isCheckBranchNameNum(@RequestParam(value="branchname", required=true) String branchname ,
                                           @RequestParam(value="bid",required = false,defaultValue = "") Integer bid) {
        System.out.println("表单校验ischeckBranchNameNum" + branchname+"bid"+bid);
        BranchVo branchVo = new BranchVo();
        branchVo.setBranchname(branchname);
        branchVo.setId(bid);
        int Checkflag = 2;
        JsonResult jsonResult = branchService.checkAddForm(branchVo, Checkflag);
        return jsonResult;
    }

    /**
     * 检查表单输入 3
     *
     * @param branchphone
     * @return
     */
    @RequestMapping("/ischeckBranchPhoneNum")
    private JsonResult ischeckBranchPhoneNum(@RequestParam(value="branchphone", required = true) String branchphone,
                                             @RequestParam(value="bid",required = false,defaultValue = "") Integer bid) {
        System.out.println("表单校验ischeckBranchPhoneNum" + branchphone+bid);
        BranchVo branchVo = new BranchVo();
        branchVo.setBphone(branchphone);
        branchVo.setId(bid);
        int Checkflag = 3;
        JsonResult jsonResult = branchService.checkAddForm(branchVo, Checkflag);
        return jsonResult;
    }
}
